package com.lantosec.hmrp.modules.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.modules.cms.entity.CmsCategory;
import com.lantosec.hmrp.modules.cms.mapper.CmsCategoryMapper;
import com.lantosec.hmrp.modules.cms.service.ICmsArticleService;
import com.lantosec.hmrp.modules.cms.service.ICmsCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.cms.vo.CmsArticleVO;
import com.lantosec.hmrp.modules.cms.vo.CmsCategoryVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * CMS栏目管理 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-07-06
 */
@Service
@Primary
@Log4j2
public class CmsCategoryServiceImpl extends ServiceImpl<CmsCategoryMapper, CmsCategory> implements ICmsCategoryService {
    @Autowired
    private ICmsArticleService iCmsArticleService;

    @Override
    public List<CmsCategoryVO> getCmsCategory(String categoryId) {
        List<CmsCategoryVO> cmsCategoryVOList = new ArrayList<>();
        List<CmsCategory> cmsCategorys = new ArrayList<>();
        if(StringUtils.isNotEmpty(categoryId)){
            cmsCategorys = this.list(new QueryWrapper<CmsCategory>().eq(CmsCategory.ColName.parent_id.toString(),categoryId).orderByAsc(CmsCategory.ColName.category_order.toString()));
        }else {
            cmsCategorys = this.list(new QueryWrapper<CmsCategory>().isNull(CmsCategory.ColName.parent_id.toString()).orderByAsc(CmsCategory.ColName.category_order.toString()));
        }
        cmsCategorys.forEach(param->{
            CmsCategoryVO cmsCategoryVO = param.convertToVO();
            List<CmsCategoryVO> cmsCategoryVOS = getCmsCategory(param.getCategoryId());
            cmsCategoryVO.setChildren(cmsCategoryVOS);
            cmsCategoryVOList.add(cmsCategoryVO);
        });
        return cmsCategoryVOList;
    }

    @Override
    public List<CmsCategoryVO> getAll(String categoryId) {
        List<CmsCategoryVO> cmsCategoryVOList = new ArrayList<>();
        List<CmsCategory> cmsCategorys = new ArrayList<>();
        if(StringUtils.isEmpty(categoryId)){
            cmsCategorys = this.list(new QueryWrapper<CmsCategory>().orderByAsc(CmsCategory.ColName.category_order.toString()));
        }else {
            cmsCategorys = this.list(new QueryWrapper<CmsCategory>().eq(categoryId != null,CmsCategory.ColName.category_id.toString(),categoryId)
                    .orderByAsc(CmsCategory.ColName.category_order.toString()));
        }
        cmsCategorys.forEach(param->{
            CmsCategoryVO cmsCategoryVO = param.convertToVO();
            //查询栏目下的文章列表
            List<CmsArticleVO> cmsArticleVOS = iCmsArticleService.getCmsArticle(param.getCategoryId(),null);
            cmsCategoryVO.setList(cmsArticleVOS);
            cmsCategoryVOList.add(cmsCategoryVO);
        });
        return cmsCategoryVOList;
    }


    @Override
    public String queryMaxId(String parentId) {
        String categoryId = null;
        if(StringUtils.isNotEmpty(parentId)){
            categoryId = baseMapper.queryMaxIdByParentId(parentId);
            return StringUtils.isEmpty(categoryId) ? parentId+"001" :categoryId;
        }else{
            categoryId = baseMapper.queryMaxId(parentId);
            return StringUtils.isEmpty(categoryId) ? "001":categoryId;
        }
    }
}
